// ******************************
// PART I: LOAD ALL DATA
// ******************************

do loadData.do

// ******************************
// PART II: GENERATE VARIABLES
// ******************************

// CLASSIFY COUNTRIES INTO DIFFERENT CATEGORIES
//quietly do auxiliary/classifyCountries.do

// TOTAL EXPENDITURE (DOMESTIC AND IMPORTS)
gen total=vdfa+vifa
gen logtotal=log(vdfa+vifa)
// DOMESTIC EXPENDITURE
gen logdomestic=log(vdfa)
// IMPORTS
gen logimports=log(vifa)
// GROSS OUTPUT
gen logGO=log(voa)
// SHARES IN GROSS OUTPUT
gen shareInGO=vdfa/voa
gen logshareInGO=log(shareInGO)
// IMPORTS AS A FRACTION OF TOTAL EXPENDITURE (by sector)
gen logimportsInTotal=log(vifa/(vifa+vdfa))
// LOG FRACTION OF TOTAL EXPENDITURE IN GO
gen fraction =total/voa
gen domfraction=vdfa/voa
gen logfraction=log(total/voa)
gen logdomfraction=log(vdfa/voa)

// US IO SHARES:
bysort upstream downstream: gen usio_=fraction if countrycode==840
bysort upstream downstream: egen usio=mean(usio_)
drop usio_


// CATEGORICAL VARIABLES
egen updown=group(upstream downstream)
egen upcountry=group(upstream countrycode)
egen downcountry=group(downstream countrycode)

// ENFORCEMENT COST VARIABLES
gen enforcingcontractsyears = enforcingcontractstimedays/365
// the standard measure of enforcement costs
gen delta_t=enforcingcontractscostofclaim/100 + enforcingcontractsyears*0.03
// with a high interest rate
gen delta_hight=enforcingcontractscostofclaim/100 + enforcingcontractsyears*0.08
// without time of enforcement
gen delta=enforcingcontractscostofclaim/100
// in logs
gen logdelta=log(delta)
gen logdeltat=log(delta_t)

// Z VARIABLES

// 1.) Construct variables required for denominator, in particular japan
gen casesPerLinkJpn=numberOfCases/links_japan
replace casesPerLinkJpn=0 if casesPerLinkJpn==.
// kick out coal, crude oil and gas, which does not really exist in Japan
replace casesPerLinkJpn = . if upstream==3 | downstream==3 | upstream==2 | downstream==2
// construct fitted number of relationships using japanese linkage data
gen logfirmsup = log(firms_japan_upstream)
gen logfirmsdown = log(firms_japan_downstream)
gen loglinks = log(links_japan)
gen logfirmsup_us = log(numberOfFirmsOrbisUpstream)
gen logfirmsdown_us = log(numberOfFirmsOrbisDownstream)
gen logfirmsup_susb_us = log(susbFirmsUpstream)
gen logfirmsdown_susb_us = log(susbFirmsDownstream)
reg loglinks logfirmsup logfirmsdown logfraction if countrycode_string=="JPN" // .69
// now coefficients are saved, create fitted number of links for the US
gen fittedlinks_us_ = _b[logfirmsup]*logfirmsup_us + _b[logfirmsdown]*logfirmsdown_us + _b[logfraction]*logfraction +_b[_cons] if countrycode==840
gen fittedlinks2_us_ = _b[logfirmsup]*logfirmsup_susb_us + _b[logfirmsdown]*logfirmsdown_susb_us + _b[logfraction]*logfraction +_b[_cons] if countrycode==840
bysort updown: egen fittedlinks_us = mean(fittedlinks_us_)
bysort updown: egen fittedlinks_susb_us = mean(fittedlinks2_us_)
drop fittedlinks_us_ fittedlinks2_us_ 

// 2.) Construct z^(1) and z^(2) by allocating the cases in each sector-pair proportionally to the 
// I-O table expenditures
preserve

drop if countrycode!=840
save __temp.dta, replace

merge m:1 upstream downstream using data/litigation.dta, keepusing(numberOfCases) 
drop _merge
rename upstream up
rename downstream down
rename up downstream
rename down upstream
rename numberOfCases numberOfCases_orig
rename vdfa vdfa_orig
merge 1:1 upstream downstream using data/litigation.dta, keepusing(numberOfCases)
rename numberOfCases numberOfCases_jux
drop _merge
merge 1:1 countrycode upstream downstream using __temp.dta, keepusing(vdfa) keep(master match)
drop _merge
rename vdfa vdfa_jux
rename upstream up
rename downstream down
rename up downstream
rename down upstream

gen totalCases = numberOfCases_orig + numberOfCases_jux
gen totalvdfa = vdfa_orig + vdfa_jux
gen partOfVdfa = vdfa_orig/totalvdfa
gen n_div_gm = totalCases*partOfVdfa / sqrt(numberOfFirmsOrbisUpstream*numberOfFirmsOrbisDownstream)
gen n_div_f = totalCases*partOfVdfa / exp(fittedlinks_us)
gen n_div_usiot = totalCases*partOfVdfa / total
gen usio_div_gm = fraction / sqrt(numberOfFirmsOrbisUpstream*numberOfFirmsOrbisDownstream)
gen usio_div_f = fraction / exp(fittedlinks_us)
// ref 3.1
gen voa_upstream_ = voa if upstream == downstream
bysort countrycode upstream : egen voa_upstream = max(voa_upstream_)
drop voa_upstream_
gen n_div_sales = totalCases * partOfVdfa / sqrt(voa_upstream * voa)

keep upstream downstream n_div_gm n_div_f n_div_usiot n_div_sales usio*
save litigiosity.dta, replace
erase __temp.dta

restore

// 3.) merge in litigiosity measures and "placebos"
merge m:1 upstream downstream using litigiosity.dta, keepusing(n_div_gm n_div_f n_div_usiot n_div_sales usio_div_gm usio_div_f)
assert _merge==3
drop _merge

// Alternative z-measures:
// numberOfCases/USIO
gen n_div_usio_= numberOfCases/vdfa if countrycode_string=="USA"
// assume plaintiff -> seller
gen n_div_gm_sellerptf_ = numberOfCases / sqrt(numberOfFirmsOrbisUpstream*numberOfFirmsOrbisDownstream)
gen n_div_f_sellerptf_ = numberOfCases / exp(fittedlinks_us)
// cases per link in japan
foreach var in n_div_usio n_div_gm_sellerptf n_div_f_sellerptf  {
	bysort upstream downstream (countrycode): egen `var'=mean(`var'_)
	drop `var'_
}

// INTERACTIONS:

// usio
gen int_usio=usio*delta
gen int_t_usio=usio*delta_t

// with z^1 and z^2
foreach zmeasure in n_div_gm n_div_f n_div_usio casesPerLinkJpn n_div_gm_sellerptf n_div_f_sellerptf usio_div_gm usio_div_f {
gen int_`zmeasure'=delta*`zmeasure'
gen int_t_`zmeasure'=delta_t*`zmeasure'
gen int_hight_`zmeasure'=delta_hight*`zmeasure'
}

// financial development and z_ni
gen intf_gm=findev*n_div_gm
gen intf_f=findev*n_div_f
gen intf_pgm = findev*usio_div_gm
gen intf_pf = findev*usio_div_f

// log gdp/capita and z_ni
gen int_lgdpc_f = loggdpc*n_div_f
gen int_lgdpc_gm = loggdpc*n_div_gm
gen int_lgdpc_pf = loggdpc*usio_div_f
gen int_lgdpc_pgm = loggdpc*usio_div_gm

// Rauch classification of goods
foreach omegameasure in con lib {
gen int_rauch_`omegameasure'=delta*(1-`omegameasure')
gen int_t_rauch_`omegameasure'=delta_t*(1-`omegameasure')
}


// PREPARE QUINTILES OF DELTAALL
xtile qtind=delta, nq(5)
forvalues i=1/5 {
gen int_q`i'_gm = (qtind==`i')*n_div_gm
gen int_q`i'_f = (qtind==`i')*n_div_f

}
drop qtind
xtile qtind=delta_t, nq(5)
forvalues i=1/5 {
gen int_qt`i'_gm = (qtind==`i')*n_div_gm
gen int_qt`i'_f = (qtind==`i')*n_div_f

}
drop qtind
// PREPARE QUINTILES OF LITIGIOSITY
xtile qtind=n_div_gm, nq(5)
forvalues i=1/5 {
gen int_q`i'gm_delta = (qtind==`i')*delta
gen int_q`i'gm_deltat = (qtind==`i')*delta_t
}
drop qtind
xtile qtind=n_div_f, nq(5)
forvalues i=1/5 {
gen int_q`i'f_delta = (qtind==`i')*delta
gen int_q`i'f_deltat = (qtind==`i')*delta_t
}
drop qtind

